package com.example.weddingapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.AndroidViewModel;

import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.base.BaseObserver;
import com.example.weddingapp.databinding.ActivityGiftBinding;
import com.example.weddingapp.utils.CommonUtil;

import java.util.List;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Date 2020/5/14 21:25
 * @Desc 送礼
 */

public class GiftActivity extends BaseActivity<AndroidViewModel, ActivityGiftBinding> {
    private int amount;
    private AVUser authorUser;
    private AVObject activity;
    private AVObject currentAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gift);
        showContentView();
        setTitle("送礼");
    }

    @Override
    protected void initData() {
        super.initData();

        String json = getIntent().getStringExtra("json");
        activity = AVUser.parseAVObject(json);
        authorUser = activity.getAVObject("user");


    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        AVUser user = AVUser.getCurrentUser();
        //查询余额
        AVQuery<AVObject> query = new AVQuery<>("Account");
        query.whereEqualTo("user", user);
        query.getFirstInBackground().subscribe(new BaseObserver<AVObject>() {
            @Override
            public void onNext(AVObject avObject) {
                super.onNext(avObject);
                currentAccount = avObject;
                amount = avObject.getInt("amount");
                double d = Double.valueOf(amount) / 100;
                String money = CommonUtil.formatAmount(d);
                bindingView.tvAmount.setText(money);
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit() {
        String trim = bindingView.etMoney.getText().toString().trim();
        try {
            Double value = Double.valueOf(trim);
            if (value <= 0) {
                showToast("金额必须大于0！");
                return;
            }

            if (value > 100000) {
                showToast("上限为10万！");
                return;
            }

            //分
            value = value * 100;
            final int intValue = value.intValue();
            if (intValue > amount) {
                showToast("余额不足，请充值");
                startActivity(new Intent(this, ValueAddActivity.class));
                return;
            }

            //个人账户余额减去相应的
            currentAccount.increment("amount", -intValue);
            currentAccount.saveInBackground().subscribe(new BaseObserver<AVObject>(){
                @Override
                public void onNext(AVObject avObject) {
                    super.onNext(avObject);
                    other(intValue);
                }
            });



        } catch (Exception e) {
            showToast("金额输入有误！");
        }
    }

    private void other(final int intValue) {
        //他人的账户余额增加
        AVQuery<AVObject> query = new AVQuery<>("Account");
        query.whereEqualTo("user", authorUser);
        query.findInBackground().subscribe(new BaseObserver<List<AVObject>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<AVObject> list) {
                if (list.size() == 0){
                    AVObject object = new AVObject("Account");
                    object.put("user", authorUser);
                    object.put("amount", intValue);
                    object.saveInBackground().subscribe(new BaseObserver<AVObject>(){
                        @Override
                        public void onNext(AVObject avObject) {
                            super.onNext(avObject);
                        }
                    });
                }else {

                    list.get(0).increment("amount",intValue);
                    list.get(0).saveInBackground().subscribe(new BaseObserver<AVObject>(){
                        @Override
                        public void onNext(AVObject avObject) {
                            super.onNext(avObject);
                        }
                    });
                }

                AVObject object = new AVObject("Order");
                object.put("action", "送礼");
                object.put("fromUser", AVUser.getCurrentUser());
                object.put("toUser",authorUser);
                object.put("activity",activity);

                object.put("intMoney", intValue);
                object.saveInBackground().subscribe(new BaseObserver<AVObject>(GiftActivity.this) {
                    @Override
                    public void onNext(AVObject avObject) {
                        super.onNext(avObject);
                        AVObject object = new AVObject("Order");
                        object.put("action", "收礼");
                        object.put("fromUser", authorUser);
                        object.put("toUser",AVUser.getCurrentUser());
                        object.put("activity",activity);

                        object.put("intMoney", intValue);
                        object.saveInBackground().subscribe(new BaseObserver<AVObject>(GiftActivity.this) {
                            @Override
                            public void onNext(AVObject avObject) {
                                super.onNext(avObject);
                                showToast("送礼成功");
                                finish();
                            }
                        });

                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
